
# coding:utf-8

from utils.suger import *
from lib.fluxext import singleton


@singleton
class Game(object):
    """游戏管理器"""
    def __init__(self):
        from views.unit import UnitManager
        from gui import UIManager
        from system.map import MapManager
        from system.AI import AISys
        from system import phy
        from system.saveload import SaveLoad
        from system.game_time import GameTime

        self.unit = UnitManager()
        self.ui = UIManager()
        self.map = MapManager()
        self.ai = AISys()
        self.phy = phy
        self.sl = SaveLoad()
        self.time = GameTime()
        self.on_control = None #当前控制的角色，即主角

        @smokesignal.on('unit_dead')
        def _callback(unit):
            if unit == self.on_control:
                Game().log.add('我挂了 - 理论上是挂了')
            else:
                Game().log.add('一个敌人挂了')

    def init(self, scr):
        self.scr = scr
        self.map.init(scr)
        self.phy.Start()
        self.ui.init()
        self.log = self.ui.log
        self.ui.AddToScreen(scr, 2147483646)

    def bind_player(self, player):
        self.on_control = player
        theCamera.SetFocus(player.anchor)
        smokesignal.emit('unit_hp_mp_changed', player)

        i = 0
        for k,v in player.skill:
            self.ui.skl[i].SetSkill(k)
            i += 1
            if i == 5:
                break

    def player_move(self, key):
        if self.on_control:
            self.on_control.Move(key)

    def player_un_move(self, key):
        if self.on_control:
            self.on_control.UnMove(key)
